<?php
// +----------------------------------------------------------------------
// | 中犇单商户
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2021 中犇科技 All rights reserved.
// +----------------------------------------------------------------------


namespace app\user\model;

use think\helper\Hash;
use think\Model as ThinkModel;


/**
 * 单页模型
 * @package app\user\model
 */
class User extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__USER__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    //会员类型0普通会员1白银会员2黄金会员
    public static $user_type = [
        0 => '普通会员',
        1 => '白银会员',
        2 => '黄金会员'
    ];
    public static $user_sex = [
        0 => '保密',
        1 => '男',
        2 => '女'
    ];
    

    /**
     * 设置器、对密码进行加密
     * 注意 仅限create/save时有作用  其他情况请自行加密
     * @param string $value 明文密码
     * @return type
     */
    public function setPasswordAttr($value)
    {
        return Hash::make((string)$value);
    }
    
    /**
     * 用户列表搜索
     * @param array $map
     * @param array $order
     * author: php迷途小书童
     * created: 2021/7/17 17:28
     */
    public static function search($map = [], $order = [], $field = '*')
    {
        // 数据列表
        $data_list = self::when(isset($map['keywords']) && $map['keywords'], function ($query) use ($map) {
            $query->where('id|user_nickname|mobile', 'like', '%'.$map['keywords'].'%');
        })
            ->when(isset($map['time']) && $map['time'], function ($query) use ($map) {
                $time = explode(' - ', $map['time']);
                $query->where('create_time', 'between time', $time);
            })
            ->when(isset($map['user_type']) && $map['user_type'] && $map['user_type'] != 'all', function ($query) use ($map) {
                $query->where('user_type', $map['user_type']);
            })
            ->when(isset($map['pids']) && $map['pids'], function ($query) use ($map) {
                $query->where('lastid', 'in', $map['pids']);
            })
            ->field($field)
            ->where('is_delete', 0)
            ->order('id', 'desc')
            ->paginate(12);
        foreach ($data_list as $k => &$item) {
            $item['head_img'] = get_file_url($item['head_img']);
        }
        unset($item);
        return $data_list;
    }

    public static function export($map = [], $order = [], $field = '*')
    {
        // 数据列表
        $data_list = self::when(isset($map['keywords']) && $map['keywords'], function ($query) use ($map) {
            $query->where('id|user_nickname|mobile', 'like', '%'.$map['keywords'].'%');
        })
            ->when(isset($map['time']) && $map['time'], function ($query) use ($map) {
                $time = explode(' - ', $map['time']);
                $query->where('create_time', 'between time', $time);
            })
            ->when(isset($map['user_type']) && $map['user_type'] && $map['user_type'] != 'all', function ($query) use ($map) {
                $query->where('user_type', $map['user_type']);
            })
            ->when(isset($map['pids']) && $map['pids'], function ($query) use ($map) {
                $query->where('lastid', 'in', $map['pids']);
            })
            ->field($field)
            ->where('is_delete', 0)
            ->order('id', 'desc')
            ->select();
        foreach ($data_list as $k => &$item) {
            $item['head_img'] = get_file_url($item['head_img']);
        }
        unset($item);
        return $data_list;
    }
}